package com.sky.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.pay.api.request.RefundRequest;
import com.sky.pay.common.WeixinRefund;
import com.sky.pay.dao.OrderDao;
import com.sky.pay.dao.RefundDao;
import com.sky.pay.service.RefundService;
import com.sky.pay.vo.Order;
import com.sky.pay.vo.Refund;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangsk
 */
@Service
@Slf4j
public class RefundServiceImpl extends ServiceImpl<RefundDao, Refund> implements RefundService {

    @Autowired
    private RefundDao refundDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Refund createRefundOrder(RefundRequest request) {
        //获取订单信息
        Order order = orderDao.getOrderByOrderNo(request.getOrderNo());

        Refund refund = new Refund();
        refund.setRefundNo("可根据自己的需求自定义生成");
        refund.setOrderNo(order.getOrderNo());
        refund.setRefundId(null);
        refund.setTotleFee(request.getTotleFee());
        refund.setRefund(request.getRefund());
        refund.setReason(request.getResson());
        Date date = new Date();
        refund.setCreateTime(date);
        refund.setUpdateTime(date);
        refundDao.insert(refund);
        return refund;
    }

    @Override
    public void updateRefundOrder(String bodyAsString) {
        Map hashMap = JSONObject.parseObject(bodyAsString, HashMap.class);

        QueryWrapper<Refund> refundQueryWrapper = new QueryWrapper<>();
        refundQueryWrapper.eq("refund_no",(String)hashMap.get("out_refund_no"));

        Refund refund = new Refund();
        refund.setRefundId((String)hashMap.get("refund_id"));
        //第一种情况：退款申请发起的退款状态
        if(!StringUtils.isEmpty((String)hashMap.get("status"))){
            refund.setRefundStatus((String)hashMap.get("status"));
            refund.setContentReturn(bodyAsString);
        }
        //第二种情况：退款回调通知的退款状态
        if(!StringUtils.isEmpty((String)hashMap.get("refund_status"))){
            refund.setRefundStatus((String)hashMap.get("refund_status"));
            refund.setContentNotify(bodyAsString);
        }
        refund.setContentNotify(bodyAsString);
        refundDao.update(refund,refundQueryWrapper);
    }

    @Override
    public List<Refund> getNoRefundOrderByDuration(int minutes) {

        //minutes 为当前时间减去5s的差值
        Instant minus = Instant.now().minus(Duration.ofMillis(minutes));
        QueryWrapper<Refund> refundQueryWrapper = new QueryWrapper<>();
        //商家退款单中，退款状态处于：“退款中”
        refundQueryWrapper.eq("refund_status", WeixinRefund.PRICESSING.getType());
        //创建订单的时间要早于5分钟之前
        refundQueryWrapper.le("create_time",minus);
        return refundDao.selectList(refundQueryWrapper);
    }
}
